package com.tim.app.ui.adapter.selceted;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.application.library.util.TimeUtil;
import com.application.library.widget.RatioImageView;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.entry.Problem;
import com.tim.app.server.entry.SelectInfo;
import com.tim.app.server.entry.User;
import com.tim.app.ui.activity.selected.QuestionAndAnswerDetailActivity;
import com.tim.app.ui.activity.some_homepage.SomeoneHomepageActivity;
import com.tim.app.util.BitmapLoader;

import java.util.List;


/**
 * 内容列表的适配器
 */
public class TopicDetailInfoItemAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, SelectInfo> {

    private Context mContext;

    private static final int TYPE_ANSWER = 1;//回答
    private static final int TYPE_PROBLEM = 2;//问题

    public TopicDetailInfoItemAdapter(Context mContext, List<SelectInfo> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        switch (viewType) {
            case TYPE_ANSWER:
                holder = new AnswerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_topic_answer, parent, false));
                break;
            case TYPE_PROBLEM:
                holder = new ProblemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_topic_problem, parent, false));
                break;
        }
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        String type = getDataList().get(position).getDiscoverType();
        if (AppKey.TYPE_ANSWER.equals(type)) {
            return TYPE_ANSWER;
        } else if (AppKey.TYPE_PROBLEM.equals(type)) {
            return TYPE_PROBLEM;
        } else {
            return TYPE_PROBLEM;
        }
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, int position, SelectInfo data) {
        if (data == null) {
            return;
        }
        if (mHolder instanceof AnswerViewHolder) {
            AnswerViewHolder answerViewHolder = (AnswerViewHolder) mHolder;
            updateAnswerViewHolder(answerViewHolder, position, data);
        } else if (mHolder instanceof ProblemViewHolder) {
            ProblemViewHolder problemViewHolder = (ProblemViewHolder) mHolder;
            updateProblemViewHolder(problemViewHolder, position, data);
        }
    }

    /**
     * 更新回答UI
     *
     * @param holder
     * @param position
     * @param data
     */
    private void updateAnswerViewHolder(AnswerViewHolder holder, final int position, final SelectInfo data) {
        if (null != data.getUserMsg()) {
            BitmapLoader.ins().loadImage(data.getUserMsg().getImage(), holder.rivAvatar);
            if (!TextUtils.isEmpty(data.getUserMsg().getName())) {
                holder.tvUserName.setText(data.getUserMsg().getName());
            }
            holder.rivAvatar.setTag(data.getUserMsg());
            holder.tvUserName.setTag(data.getUserMsg());
            holder.rivAvatar.setOnClickListener(onClickListener);
            holder.tvUserName.setOnClickListener(onClickListener);
        }
        if (null != data.getImageUrl()) {
            List<String> dataImageUrl = data.getImageUrl();
            holder.llImageContainer.setVisibility(View.VISIBLE);
            if (data.getImageUrl().size() == 1) {
                holder.llMoreImageContainer.setVisibility(View.GONE);
                holder.rivCover.setVisibility(View.VISIBLE);
                BitmapLoader.ins().loadImage(dataImageUrl.get(0), holder.rivCover);
            } else {
                holder.llMoreImageContainer.setVisibility(View.VISIBLE);
                holder.rivCover.setVisibility(View.GONE);
                BitmapLoader.ins().loadImage(dataImageUrl.get(0), holder.rivCoverOne);
                if (dataImageUrl.size() > 1) {
                    BitmapLoader.ins().loadImage(dataImageUrl.get(1), holder.rivCoverTwo);
                }
                if (dataImageUrl.size() > 2) {
                    BitmapLoader.ins().loadImage(dataImageUrl.get(2), holder.rivCoverThree);
                }
            }
        } else {
            holder.llImageContainer.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(data.getProblemTitle())) {
            holder.tvTitle.setText(data.getProblemTitle());
        } else {
            if (!TextUtils.isEmpty(data.getTitle())) {
                holder.tvTitle.setText(data.getTitle());
            }
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvContent.setText(data.getContent());
        }

        holder.tvTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Problem problem = new Problem();
                problem.setId(String.valueOf(data.getProblemId()));
                QuestionAndAnswerDetailActivity.start(mContext, problem, false);
            }
        });
        holder.tvPriseNumber.setText(mContext.getString(R.string.selectAnswerPriseNumber, String.valueOf(data.getAgreeCount())));
        holder.tvCommentNumber.setText(mContext.getString(R.string.selectCommentNumber, String.valueOf(data.getCommentCount())));
        holder.tvPublishTime.setText(TimeUtil.formatTimeRuleTwo(mContext, data.getCreateTime()) + mContext.getString(R.string.pointPrompt));
    }

    /**
     * 更新问题UI
     *
     * @param holder
     * @param position
     * @param data
     */
    private void updateProblemViewHolder(ProblemViewHolder holder, int position, final SelectInfo data) {
        if (null != data.getUserMsg()) {
            BitmapLoader.ins().loadImage(data.getUserMsg().getImage(), holder.rivAvatar);
            if (!TextUtils.isEmpty(data.getUserMsg().getName())) {
                holder.tvUserName.setText(data.getUserMsg().getName());
            }
            holder.rivAvatar.setTag(data.getUserMsg());
            holder.tvUserName.setTag(data.getUserMsg());
            holder.rivAvatar.setOnClickListener(onClickListener);
            holder.tvUserName.setOnClickListener(onClickListener);
        }

        if (null != data.getImageUrl()) {
            holder.llImageContainer.setVisibility(View.VISIBLE);
            if (data.getImageUrl().size() == 1) {
                holder.llMoreImageContainer.setVisibility(View.GONE);
                holder.rivCover.setVisibility(View.VISIBLE);
                BitmapLoader.ins().loadImage(data.getImageUrl().get(0), holder.rivCover);
            } else {
                holder.llMoreImageContainer.setVisibility(View.VISIBLE);
                holder.rivCover.setVisibility(View.GONE);
                BitmapLoader.ins().loadImage(data.getImageUrl().get(0), holder.rivCoverOne);
                BitmapLoader.ins().loadImage(data.getImageUrl().get(1), holder.rivCoverTwo);
                BitmapLoader.ins().loadImage(data.getImageUrl().get(2), holder.rivCoverThree);
            }
        } else {
            holder.llImageContainer.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvTitle.setText(data.getTitle());
        } else {
            if (!TextUtils.isEmpty(data.getProblemTitle())) {
                holder.tvTitle.setText(data.getProblemTitle());
            }
        }
        holder.tvFollowNumber.setText(mContext.getString(R.string.selectFollowNumber, String.valueOf(data.getConcernCount())));
        holder.tvAnswerNumber.setText(mContext.getString(R.string.selectAnswerNumber, String.valueOf(data.getAnswerCount())));
        holder.tvPublishTime.setText(TimeUtil.formatTimeRuleTwo(mContext, data.getCreateTime()) + mContext.getString(R.string.pointPrompt));
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            User user = (User) v.getTag();
            if (null != user)
                SomeoneHomepageActivity.start(mContext, user, false);
        }
    };


    /**
     * 回答
     */
    class AnswerViewHolder extends BaseRecyclerViewHolder {

        TextView tvFromSuject;
        RoundedImageView rivAvatar;
        TextView tvUserName;
        TextView tvTitle;
        TextView tvContent;
        TextView tvPriseNumber;
        TextView tvCommentNumber;
        LinearLayout llImageContainer;
        RatioImageView rivCover;
        LinearLayout llMoreImageContainer;
        RatioImageView rivCoverOne;
        RatioImageView rivCoverTwo;
        RatioImageView rivCoverThree;
        TextView tvPublishTime;

        public AnswerViewHolder(View itemView) {
            super(itemView);
            tvFromSuject = (TextView) itemView.findViewById(R.id.tvFromSuject);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvContent = (TextView) itemView.findViewById(R.id.tvContent);
            tvPriseNumber = (TextView) itemView.findViewById(R.id.tvPriseNumber);
            tvCommentNumber = (TextView) itemView.findViewById(R.id.tvCommentNumber);
            llImageContainer = (LinearLayout) itemView.findViewById(R.id.llImageContainer);
            rivCover = (RatioImageView) itemView.findViewById(R.id.rivCover);
            llMoreImageContainer = (LinearLayout) itemView.findViewById(R.id.llMoreImageContainer);
            rivCoverOne = (RatioImageView) itemView.findViewById(R.id.rivCoverOne);
            rivCoverTwo = (RatioImageView) itemView.findViewById(R.id.rivCoverTwo);
            rivCoverThree = (RatioImageView) itemView.findViewById(R.id.rivCoverThree);
            tvPublishTime = (TextView) itemView.findViewById(R.id.tvPublishTime);
        }

    }

    /**
     * 问题
     */
    class ProblemViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivAvatar;
        TextView tvUserName;
        TextView tvFromSuject;
        TextView tvTitle;
        TextView tvFollowNumber;
        TextView tvAnswerNumber;
        LinearLayout llImageContainer;
        RatioImageView rivCover;
        LinearLayout llMoreImageContainer;
        RatioImageView rivCoverOne;
        RatioImageView rivCoverTwo;
        RatioImageView rivCoverThree;
        TextView tvPublishTime;

        public ProblemViewHolder(View itemView) {
            super(itemView);
            tvFromSuject = (TextView) itemView.findViewById(R.id.tvFromSuject);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvFollowNumber = (TextView) itemView.findViewById(R.id.tvFollowNumber);
            tvAnswerNumber = (TextView) itemView.findViewById(R.id.tvAnswerNumber);
            llImageContainer = (LinearLayout) itemView.findViewById(R.id.llImageContainer);
            rivCover = (RatioImageView) itemView.findViewById(R.id.rivCover);
            llMoreImageContainer = (LinearLayout) itemView.findViewById(R.id.llMoreImageContainer);
            rivCoverOne = (RatioImageView) itemView.findViewById(R.id.rivCoverOne);
            rivCoverTwo = (RatioImageView) itemView.findViewById(R.id.rivCoverTwo);
            rivCoverThree = (RatioImageView) itemView.findViewById(R.id.rivCoverThree);
            tvPublishTime = (TextView) itemView.findViewById(R.id.tvPublishTime);
        }

    }


}
